package top.xchch.workdemo.func02;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程同步方式二
 *  wait和notify同步让主线程等待任务线程的执行
 *
 * @author sai.zhang
 * @date 2021-10-17 19:29
 */
public class Main {
    private static ExecutorService es = Executors.newFixedThreadPool(4);

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();

        ThirdObj thirdObj = new ThirdObj();
        es.submit(() -> {
            thirdObj.task();
        });

        thirdObj.get();
        System.out.println("使用时间: "+ (System.currentTimeMillis() - start) + " ms");

        es.shutdown();
    }
}

class ThirdObj {
    public synchronized void get() throws InterruptedException {
        wait();
    }

    private int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }

    public synchronized void task() {
        System.out.println("计算结果: " + fibo(36));
        notify();
    }
}
